package com.lx.lib.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lx.commonlib.mvp.persenter.BasePresenter;
import com.lx.commonlib.mvp.view.activity.ToolbarActivity;
import com.lx.lib.R;

public class MainActivity extends ToolbarActivity {

    private ListView mListView;

    String[] strs=new String[]{
            "ThreadUtil",
            "UIHandlerUtil",
            "LogAndToast",
            "ToolbarFragmentActivity"
    };


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public boolean isCanGoBack() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("首页");

        mListView = (ListView) findViewById(R.id.listView);

        mListView.setAdapter(new ListViewAdapter(strs, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (int) view.getTag();
                switch (position){
                    case 0: // ThreadUtil
                        Intent intent = new Intent(MainActivity.this, ThreadUtilsActivity.class);
                        startActivity(intent);
                        break;
                    case 1: // UIHandlerUtil
                        intent = new Intent(MainActivity.this, UIHandlerActivity.class);
                        startActivity(intent);
                        break;
                    case 2: // LogAndToast
                        intent = new Intent(MainActivity.this, LogAndToastActivity.class);
                        startActivity(intent);
                        break;
                    case 3: // SingleClick、ToolbarFragment
                        intent = new Intent(MainActivity.this, ToolbarFragmentActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        }));

    }



    class ListViewAdapter extends BaseAdapter{
        private String[] data;
        private View.OnClickListener mListener;
        private LayoutInflater mInflater;

        public ListViewAdapter(String[] strs, View.OnClickListener listener){
            this.data=strs;
            mListener = listener;
            mInflater = LayoutInflater.from(getApplicationContext());
        }

        @Override
        public int getCount() {
            return data==null?0:data.length;
        }

        @Override
        public Object getItem(int i) {
            return data[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int positon, View view, ViewGroup viewGroup) {
            if(view==null){
                View inflate = mInflater.inflate(R.layout.item_main_listview, viewGroup, false);
                TextView textView = inflate.findViewById(R.id.text_view);
                textView.setText(data[positon]);
                view=inflate;
            }else{
                TextView textView = view.findViewById(R.id.text_view);
                textView.setText(data[positon]);
            }

            view.findViewById(R.id.v_cover).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mListener!=null){
                        view.setTag(positon);
                        mListener.onClick(view);
                    }
                }
            });

            return view;
        }
    }

}
